package com.bookseek.xreader;

import android.app.Application;
import android.content.Context;
import android.util.Log;

import androidx.lifecycle.Observer;
import androidx.work.BackoffPolicy;
import androidx.work.Constraints;
import androidx.work.Data;
import androidx.work.ExistingWorkPolicy;
import androidx.work.NetworkType;
import androidx.work.OneTimeWorkRequest;
import androidx.work.WorkInfo;
import androidx.work.WorkManager;

import com.blankj.utilcode.util.DeviceUtils;
import com.blankj.utilcode.util.SPUtils;
import com.bookseek.xreader.consts.XreaderConts;
import com.bookseek.xreader.consts.XreaderHolder;
import com.bookseek.xreader.dbhelper.DbSessionUtils;
import com.bookseek.xreader.fucker.MetaWorker;
import com.bookseek.xreader.service.DataService;
import com.bookseek.xreader.service.dk.TMetaService;
import com.bookseek.xreader.utils.DLog;
import com.bookseek.xreader.utils.NbUtils;
import com.bookseek.xreader.utils.OkHttpUtils;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

import jackmego.com.jieba_android.JiebaSegmenter;

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        Log.i("X_APP","初始化============》");
        super.onCreate();
        Context context = getApplicationContext();
        // 异步初始化 结巴分词
        JiebaSegmenter.init(context);
        //sqlite3
        DbSessionUtils.initV1(context,"jdk18");
        //App配置
        DLog.info(XreaderConts.TAG,"加载 cfg");
        DataService.getInstance().initAppCfg(cfg->{
            DLog.info(XreaderConts.TAG,"CFG=>"+ OkHttpUtils.gson.toJson(cfg));
            XreaderHolder.CFG = cfg;
            TMetaService.getInstance().saveTMetaInfo(cfg);

            runOnceWorker(context,XreaderConts.TMETA_BTYPE_DIANJI,"bdoc_work_dianji");
            runOnceWorker(context,XreaderConts.TMETA_BTYPE_SHICI,"bdoc_work_shici");
        });

        initDeviceInfo();

    }

    public void initDeviceInfo(){
        String uuid = NbUtils.getDeviceUniqId();
        DLog.info("设备唯一标识--》",uuid,"设备厂商=》", DeviceUtils.getManufacturer()," 是否是模拟器-》",DeviceUtils.isEmulator());
        XreaderHolder.UID = uuid;
        XreaderHolder.MID = NbUtils.getMid();
    }
    public void runOnceWorker(Context context,Integer btype,String uniqueWorkName){
        // a. 定义任务执行的约束条件（可选）
        Constraints constraints = new Constraints.Builder()
                .setRequiredNetworkType(NetworkType.CONNECTED) // 仅在有网络时执行
                .build();
        //诗词
        Data shiciParam = new Data.Builder()
                .putInt("btype", btype)
                .build();
        OneTimeWorkRequest shiciWorkRequest = new OneTimeWorkRequest.Builder(MetaWorker.class)
                .setInputData(shiciParam)
                .setConstraints(constraints) // 设置约束条件
                .setInitialDelay(3, TimeUnit.SECONDS) // 延迟3秒执行
                .setBackoffCriteria(
                        BackoffPolicy.LINEAR, // 重试策略：线性延迟
                        5, TimeUnit.SECONDS // 重试间隔：
                )
                .build();
        WorkManager.getInstance(context).enqueueUniqueWork(
                uniqueWorkName,
                ExistingWorkPolicy.REPLACE,
                shiciWorkRequest
                );
        UUID workid = shiciWorkRequest.getId();
        XreaderHolder.SHICI_ID = workid;
        //典籍
        DLog.info("3s 后开始同步任务===》",workid);
    }


}
